const Mock = require('mockjs')


const myBusinessData = Mock.mock({
  'data|12': [
    {
      "businessId|+1": 1,
      "orderTypeId|+1": 1,
      "businessName": "@cword(5)",
      "businessAddress": "@cword(5)",
      "businessExplain": "@cword(5)",
      "businessImg": "@image('50x50', '#4A7BF7', '商家名称')",
      "orderTypeId": '@integer(1,30)',
      "starPrice": '@float(15,50,0,2)',
      "deliveryPrice": "@float(3,20,0,2)",
      "remarks": "@csentence(3, 5)",
    }
  ]
})


module.exports = [
  // 获取商家列表
  {
    url: '/vue-admin-template/business/list',
    type: 'get',
    response: config => {

      const params = config.query;
      // console.log("-----mock food data: %o", myBusinessData.data);
      console.log("-----商家列表接口，请求参数:%o", params);
      const bId = (undefined == params.orderTypeId || params.orderTypeId.trim() == "") ? null : params.orderTypeId.trim();
      const bName = (undefined == params.businessName || params.businessName.trim() == "") ? null : params.businessName.trim();

      // +  把字符串转换成整数类型
      const pageNo = +params.page;  //页码
      const pageSize = +params.limit; //每页大小

      let retData = [];

      //处理:非分页的查询参数
      if (bId != null || bName != null) {
        for (let x of myBusinessData.data) {
          // console.log("-----bId:%d,bName:%s,x:%o,orderTypeId:%s", bId, bName, x, (bId == x.orderTypeId) + "");

          // 1.只输入：商家编号
          if ((bId != null && bId == x.orderTypeId) && (bName == null)) {
            retData.push(x);
          }
          // 2.只输入：商家名称
          if ((bId == null) &&
            (bName != null && x.businessName.indexOf(bName) != -1)) {
            retData.push(x);
          }

          // 3.输入的是：商家编号 和 商家名称
          if ((bId != null && bId == x.orderTypeId)
            && (bName != null && x.businessName.indexOf(bName) != -1)) {
            retData.push(x);
          }
        }
      } else {
        retData = myBusinessData.data;
      }

      //处理:分页问题（将查询出来的结果，按分页参数要求展示适当的数据）
      let startIndex = (pageNo - 1) * pageSize;
      let endIndex = startIndex + pageSize;
      console.log(`----startIndex:${startIndex}.endIndex:${endIndex}`);
      retData = retData.slice(startIndex, endIndex);

      return {
        code: 20000,
        data: {
          "data": retData,
          "total": 12
        }
      }
    }
  },

  //根据id删除商家信息
  {
    url: '/vue-admin-template/business/delete',
    type: 'post',
    response: config => {
      const params = config.body;
      console.log("-----商家列表接口，请求参数:%o", params);
      let foodIndex = -1; //商家数组中的商家索引
      myBusinessData.data.forEach((x, index) => {
        if (x.orderTypeId == params.orderTypeId) {
          foodIndex = index;
        }
      });
      //将该商家信息从商家列表中删除
      myBusinessData.data.splice(foodIndex, 1);

      return {
        code: 20000,
        data: "删除商家成功！"
      }
    }
  },

  // 根据商家id获取商家信息
  {
    url: '/vue-admin-template/business/detail',
    type: 'get',
    response: config => {
      const params = config.query;
      console.log("-----商家详情接口，请求参数:%o", params);
      let retData = null;
      myBusinessData.data.forEach(x => {
        if (x.businessId == params.id) {
          retData = x
        }
      });

      return {
        code: 20000,
        data: {
          "business": retData,

        }
      }
    }
  },

  //更新食品信息
  {
    url: '/vue-admin-template/business/update',
    type: 'post',
    response: config => {
      let params = config.body;
      console.log("-----更新食品接口，请求参数:%o", params);
      myBusinessData.data.forEach(item => {
        if (item.orderTypeId == params.business.orderTypeId) {
          Object.assign(item, params.business);
        }
      });

      return {
        code: 20000,
        data: "上架成功"
      }
    }
  },

  //上架商家
  {
    url: '/vue-admin-template/business/create',
    type: 'post',
    response: config => {
      const business = config.body;
      console.log(myBusinessData.data);

      myBusinessData.data.push(business)

      return {
        code: 20000,
        data: "success"
      }
    }
  },


]
